#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "space_war.h"

void set_operation(state p1, state p2, resource_data resources, operation *ops);
void move_all(state p1, state p2, resource_data resources, operation *ops);
void clear_all(state p1, state p2, resource_data resources, operation *ops);
void ops_to_0(operation *ops);
void suicide(state p1, operation *ops);
int main (int argc, char const *argv[])
{
    //玩家状态
    state p1, p2;
    //垃圾位置
    resource_data resources;
    /*-----------------------此为交互文件路径设置-----------------------*/
    char res_path[] = "C:\\space_war\\p1_request.txt";
    char cmd_path[] = "C:\\space_war\\p1_command.txt";
    char flag;
    if(argc == 2){
        flag = argv[1][0];
        if(flag != '0' && flag != '1'){
            fprintf(stderr, "命令行参数错误\n");
            exit(1);
        }
    }
    else if (argc == 1){
        flag = '0';
    }
    else{
        fprintf(stderr, "命令行参数过多\n");
        exit(1);
    }
    set_path(res_path, cmd_path, flag);
    /*----------------------------请勿更改----------------------------*/
    p1.station_pos.x = 0;
    p1.station_pos.y = 0;

    operation ops;

    int result = 0;
    while (1)
    {
        if((result = is_end(res_path)) == 0){
            if(is_refresh(res_path, p1.station_pos)){
                if(load_state(&p1, &p2, &resources, res_path, flag)){
                    set_operation(p1, p2, resources, &ops);
                }

                // p1.attack_number = attack_max;
                // p1.clear_number = clear_max;
                send_cmd(p1, ops, cmd_path);
                // printf("%s:refreshed\n",res_path);
            }
        }
        else{
            int actual_player = flag - '0' + 1;
            if(actual_player == result){
                printf("%s:You Win!:)\n", res_path);
            }
            else{
                printf("%s:You Lose:(\n", res_path);
            }
            break;
        }

    }
    
    return 0;
}

void set_operation(state p1, state p2, resource_data resources ,operation *ops){
    ops_to_0(ops);
    if(p1.station_pos.r == 9){
        ops->station_move = 8;
    }
    else if (p1.station_pos.r == 8 && p1.attack_number == 0)
    {
        ops->station_ops = 1;
        ops->station_move = 0;
    }
    else if (p1.station_pos.r == 8 && p1.attack_number == 1)
    {
        suicide(p1, ops);
    }
}
void ops_to_0(operation *ops){
    ops->station_ops = 0;
    ops->station_move = 0;
    for (int i = 0; i < attack_max; i++){
        ops->attack_ops[i] = 0;
    }
    for (int i = 0; i < clear_max; i++){
        ops->clear_ops[i] = 0;
    }
    
}
void move_all(state p1, state p2, resource_data resources,operation *ops){
    for (int i = 0; i < p1.clear_number;i++){
        if(p1.clear_units[i].r == p1.station_pos.r){
            ops->clear_ops[i] = ops->clear_ops[i] + (p1.station_pos.r != 9) ? 1 : (-1);
        }
    }
}
void clear_all(state p1, state p2, resource_data resources,operation *ops){
    
    for (int i = 0; i < p1.clear_number && i < resources.num; i++){
        ops->clear_ops[i] = resources.pos[i].r;
    }
}

void suicide(state p1, operation *ops){
    pos attacker = p1.attack_units[0];
    pos target = p1.station_pos;
    if(attacker.r != target.r -1){
        ops->attack_ops[0] = target.r - 1;
    }
    else{
        double delta_theta = target.theta - attacker.theta;
        // printf("%lf:%lf\n", attacker.theta, target.theta);
        if(delta_theta < -0.2 && delta_theta > -0.3){
            ops->attack_ops[0] = target.r;
            printf("attacker:%lf; target:%lf; delta:%lf\n", attacker.theta, target.theta, delta_theta);
        }
    }
}
